# Required functions: `strs.total`, `srs.total` uspop=read.table("http://www.webpages.uidaho.edu/%7Erenaes/Data/R%20Data%20files/USpop.txt",sep='',header=T) View(uspop) set.seed(59373); library(sampling) # and for some reason, there are errors in the dataset but now is a great time # to illustrate how to remove and change values in the dataset from R (it will not change the original data file) # because the first row is the US information for all states combined uspop=uspop[-1,] # Delaware's total population and Y1824 values are wrong # 1. Find Delaware in the list # 2. Change the corresponding elements of the variables uspop[uspop$State=='Delaware',] # The 2nd variable (Total=11821) and 4th variable (Y1824=11821) # in fact, you can see that 11821 is the value for all the variables of this state # We will change Total and Y1824 and just use those uspop[uspop$State=='Delaware','Total']<-807385 uspop[uspop$State=='Delaware','Y1824']<-81501 # changes made uspop[uspop$State=='Delaware',] # Create new variable (strata) based on population uspop$stratum<-ifelse(uspop$Total<3000000,1,ifelse(uspop$Total>10000000,3,2)) Ni=c(sum(uspop$stratum==1),sum(uspop$stratum==2),sum(uspop$stratum==3)) ni=rep(5,3) uspop=uspop[order(uspop$stratum),] strs=strata(uspop,stratanames=c("stratum"),size=ni, method="srswor") uspop.strs=getdata(uspop,strs) tauhat.strs('uspop.strs',uspop.strs$Y1824,uspop.strs$stratum,ni,Ni) # compare to SRS N=50; n=15 tauhat.srs('uspop.strs',uspop.srs$Pop18_24,n,N)